package com.tang.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.tang.common.enums.DeleteEnum;
import com.tang.subject.domain.convert.SubjectLabelConverter;
import com.tang.subject.domain.convert.SubjectMappingConverter;
import com.tang.subject.domain.entity.SubjectLabelBO;
import com.tang.subject.domain.service.SubjectLabelDomainService;
import com.tang.subject.infra.basic.entity.pojo.SubjectCategory;
import com.tang.subject.infra.basic.entity.pojo.SubjectLabel;
import com.tang.subject.infra.basic.entity.pojo.SubjectMapping;
import com.tang.subject.infra.basic.service.SubjectCategoryService;
import com.tang.subject.infra.basic.service.SubjectLabelService;
import com.tang.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    @Resource
    private SubjectLabelService subjectLabelService;
    @Resource
    private SubjectMappingService subjectMappingService;
    @Resource
    private SubjectCategoryService subjectCategoryService;

    /**
     * 添加标签
     *
     * @param subjectLabelBO
     * @return void
     * @author Tang
     * @date 2024/2/1 21:22
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addLabel(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainService.addLabel.bo:{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(DeleteEnum.UN_DELETED.code);
        subjectLabelService.insert(subjectLabel);
        SubjectMapping subjectMapping = SubjectMappingConverter.INSTANCE.convertLabelBoToSubjectMapping(subjectLabelBO);
        subjectMapping.setLabelId(subjectLabel.getId());
        subjectMappingService.insert(subjectMapping);
        return true;
    }

    /**
     * 更新标签
     *
     * @param subjectLabelBO
     * @return java.lang.Boolean
     * @author Tang
     * @date 2024/2/1 21:47
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateLabel(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainService.updateLabel.bo:{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        return subjectLabelService.update(subjectLabel) > 0;
    }

    /**
     * 删除标签
     *
     * @param subjectLabelBO 待删除的标签信息
     * @return java.lang.Boolean
     * @author Tang
     * @date 2024/2/2 13:29
     **/
    @Override
    public Boolean deleteLabel(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainService.deleteLabel.bo:{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBoToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(DeleteEnum.DELETE.code);
        return subjectLabelService.update(subjectLabel) > 0;
    }

    /**
     * 查询分类下的标签
     *
     * @param subjectLabelBO 标签查询条件
     * @return java.util.List<com.tang.subject.domain.entity.SubjectLabelBO>
     * @author Tang
     * @date 2024/2/2 14:45
     **/
    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainService.queryLabel.bo:{}", JSON.toJSONString(subjectLabelBO));
        }
        //查询当前分类的子分类
        List<SubjectCategory> subjectCategories = subjectCategoryService.querySonIdsByParentId(subjectLabelBO.getCategoryId());
        List<Long> categoryIds = subjectCategories.stream().map(SubjectCategory::getId).collect(Collectors.toList());
        //查询标签和分类的关系
        List<SubjectMapping> subjectMappings = subjectMappingService.queryBatch(categoryIds);
        List<Long> collect = subjectMappings.stream()
                .map(SubjectMapping::getLabelId)
                .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(collect)) {
            return Collections.emptyList();
        }
        //批量查询
        List<SubjectLabel> subjectLabels = subjectLabelService.queryBatch(collect);
        //包装
        return subjectLabels.stream().map(i -> {
            SubjectLabelBO labelBO = SubjectLabelConverter.INSTANCE.convertLabelToBo(i);
            labelBO.setCategoryId(subjectLabelBO.getCategoryId());
            return labelBO;
        }).collect(Collectors.toList());
    }
}
